package exam.controller;



import exam.bean.PatentInfo;
import exam.service.IPatentInfoService;
import exam.utils.FileUtil;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController()
@RequestMapping("/PatentInfo")
public class PatentInfoController {
    @Autowired
    IPatentInfoService patentInfoService;

    @Value("${upload.uploadPath}")
    private String uploadPath;

    @ResponseBody
    @GetMapping("/PatentInfoAll")
    public Msg paperInfo(
            @RequestParam(value="pageNum",defaultValue="1") int pageNum,
            @RequestParam(value="pageSize",defaultValue = "10") int pageSize,
            @RequestParam(value = "patentName",defaultValue = "") String patentName){
        Map<String,Object> map = new HashMap<>();
        map.put("patentName",patentName);
        return Msg.success().add("pageInfo",patentInfoService.selectInfo(pageNum,pageSize,map));
    }


    @PostMapping("/searchPatentInfo/{pageNum}/{pageSize}")
    public Msg searchPatentInfo(@PathVariable String pageNum,@PathVariable String pageSize,@RequestBody Map queryMap){
        return Msg.success().add("patentPageInfo",patentInfoService.selectInfo(Integer.parseInt(pageNum),
                Integer.parseInt(pageSize),queryMap));
    }

    @PostMapping("/add")
    public Msg add(@RequestPart("patentInfo") PatentInfo patentInfo,
                   @RequestPart(value = "files",required = false)MultipartFile[] files){
        patentInfo.setFiles(Arrays.asList(files));
        boolean re = patentInfoService.insertInfo(patentInfo,uploadPath) > 0 ;
        return Msg.success().setMessage(re ? "添加成功" : "添加失败");
    }

    @ResponseBody
    @GetMapping("/detail")
    public Msg detail(@RequestParam(value="id") int id){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",id);
        return Msg.success().add("patentInfo",patentInfoService.selectInfoSingle(map));
    }

    @ResponseBody
    @GetMapping("/delete")
    public Msg delete (@RequestParam(value="id") int id){
        boolean re = patentInfoService.deleteInfo(id) > 0 ;
        return Msg.success().setMessage(re ? "删除成功" : "删除失败");
    }

    @ResponseBody
    @PostMapping("/upData")
    public Msg update(@RequestPart("patentInfo") PatentInfo patentInfo,
                      @RequestPart(value = "files",required = false) MultipartFile[] files){
        patentInfo.setFiles(Arrays.asList(files));
        boolean re = patentInfoService.updateInfo(patentInfo,uploadPath) > 0;
        return Msg.success().setMessage( re ? "更新成功" : "更新失败");
    }

    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response, String fileName, String fileNameSrc){
        FileUtil.downLoadFile(response,uploadPath,fileName,fileNameSrc);
    }
}
